using System;
using System.Collections.Generic;
using System.Text;
using SCG.SolarVengeanceEngine;
using SCG.SolarVengeanceEngine.BrainInfo;

namespace SCG.SolarVengeance.Brains
{

    public class GoldenDragon : Brain
    {
        private Dictionary<string, StarSystemInfo> _WarshipTarget = new Dictionary<string, StarSystemInfo>();
        private Dictionary<StarSystemInfo, int> _ShieldsLeft = new Dictionary<StarSystemInfo, int>();

        public override ChallengeLevel Challenge
        {
            get
            {
                return ChallengeLevel.Moderate;
            }
        }

        public override string Author
        {
            get { return "Ugly Green Things"; }
        }

        public override string Description
        {
            get
            {
                return "Golden Dragon V1.0.0.0 Primary coding is to assault StarSystems with many WarShips, and builds Techships" +
                    "to support that effort.  "; 
            }
        }

        //private members
        private int MaxWeaponValue = 0;
        private int MinWpn = 5;
        private bool CloseCaps = false;
        private bool CapDefense = true;
        private double CapDist = 0;

        //Establish base list of ships that the brain can build
        public override List<StarShipType> AvailableShipTypes
        {
            get
            {
                List<StarShipType> shipTypes = new List<StarShipType>();
                shipTypes.Add(StarShipType.WarShip);
                shipTypes.Add(StarShipType.Drone);
                shipTypes.Add(StarShipType.TechShip);
                shipTypes.Add(StarShipType.StarTillery);
                shipTypes.Add(StarShipType.InkSpot);
                shipTypes.Add(StarShipType.SpyShip);
                return shipTypes;
            }
        }

        //Executable Brain Logic
        public override void Execute()
        {
            //bool CCDE = false;
            //bool WshPlt6 = false;
            //bool WshPgt6 = false;
            //bool NshPlt6 = false;
            //bool NshPgt6 = false;
            //bool ElsCond = false;

            int sysValue = 0;
            int CapX = -1;
            int CapY = -1;
            int ECapX = -1;
            int ECapY = -1;

            //----Build Warship Targeting Tables--------------------------------------------------------------

            int Mystars = 0;
            bool expanded = true;
            foreach (StarSystemInfo s in StarSystems)
            {
                if (_ShieldsLeft.ContainsKey(s) == false)
                {
                    _ShieldsLeft.Add(s, 9);
                }
                if (s.OwnedByMe == false)
                {
                    if (s.Visible == true)
                    {
                        _ShieldsLeft[s] = s.Shields;
                    }
                    else
                    {
                        _ShieldsLeft[s] = 9;
                    }
                }
                else
                {
                    Mystars++;
                    _ShieldsLeft[s] = 9;
                }
            }

            if ((Mystars < StarSystems.Count / 10) | (Mystars < 10))
                expanded = true;
            else
                expanded = false;

            foreach (StarShipInfo sh in MyStarShips)
            {
                if (sh.ShipType == StarShipType.WarShip)
                {
                    if (_WarshipTarget.ContainsKey(sh.Code) == false)
                        _WarshipTarget.Add(sh.Code, null);
                    else
                    {
                        if (_WarshipTarget[sh.Code] != null)
                        {
                            StarSystemInfo sys = _WarshipTarget[sh.Code];
                            if (sys.OwnedByMe == true)
                                _WarshipTarget[sh.Code] = null;
                        }
                    }
                }
            }


            //----End Warship Targeting Tables------------------------------------------------------------------------------

            foreach (StarSystemInfo sys in StarSystems)
            {
                if (sys.OwnedByMe)
                {
                    if (sys.IsCapital)
                    {
                        CapX = Capital.X;
                        CapY = Capital.Y;
                    }
                    //---------------Count StarSystem Values 
                    else if (!sys.IsCapital)
                        sysValue += sys.Value;
                }
                //-----------------------Close Capital Identification
                if (sys.IsCapital && !sys.OwnedByMe)
                {
                    ECapX = sys.X;
                    ECapY = sys.Y;

                    CapDist = Distance(CapX, CapY, ECapX, ECapY);

                    if (CapDist <= 10)
                        CloseCaps = true;
                    else
                        CloseCaps = false;

                }
            }


            int NumTech = 0;
            int NumWarShip = 0;
            int NumStarTillery = 0;
            int NumSpyShips = 0;
            int STtoSSratio = 0;

            foreach (StarShipInfo sh in StarShips)
            {
                //-----------------------Count Ship Types Built
                if (sh.OwnedByMe)
                {
                    if (sh.ShipType == StarShipType.TechShip)
                        NumTech++;
                    if (sh.ShipType == StarShipType.WarShip)
                        NumWarShip++;
                    if (sh.ShipType == StarShipType.StarTillery)
                        NumStarTillery++;
                    if (sh.ShipType == StarShipType.SpyShip)
                        NumSpyShips++;
                    STtoSSratio = (NumStarTillery + 1) / (NumSpyShips + 1);
                }
                //Warship Intelligence Section
                else if (sh.OwnedByMe == false)
                {
                    if ((sh.IdentifiedByMe == false) | (sh.ShipType == StarShipType.WarShip))
                        if (sh.KnownWeapons >= MinWpn)
                        {
                            MaxWeaponValue = sh.KnownWeapons;
                            MinWpn = MaxWeaponValue + 1;
                        }
                    if (sh.Value >= MinWpn)
                    {
                        MaxWeaponValue = sh.Value;
                        MinWpn = MaxWeaponValue + 1;
                    }
                }
            }

            //-----------------------------------StarSystem Build Logic---------------------------------------------------------------------

            foreach (StarSystemInfo sys in StarSystems)

                if (sys.OwnedByMe)
                {
                    if (sys.IsCapital)
                    {
                        //Close Capital Attack/Defense
                        if (sysValue <= 80 && CloseCaps == true)
                        {
                            //CCDE = true;
                            if (CapDist < 5 && CapDefense == true)
                            {
                                SetAutoStarShip(sys, false);
                                BuildShields(sys, 4);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.WarShip, 5, 2, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.WarShip, 5, 2, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.WarShip, 5, 2, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.WarShip, 5, 2, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                CapDefense = false;
                            }

                            if (CapDist >= 5 && CapDefense == true)
                            {
                                SetAutoStarShip(sys, false);
                                BuildShields(sys, 4);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.WarShip, 5, 2, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                BuildStarShip(sys, StarShipType.WarShip, 5, 2, false);
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                                CapDefense = false;
                            }

                            StarShipInfo AttackingShip = ClosestStarShip(sys.X, sys.Y, 5, FindShipFlags.Enemy, ShipKnownFlags.Either);
                            StarShipInfo CloseShip = ClosestStarShip(sys.X, sys.Y, 8, FindShipFlags.Enemy, ShipKnownFlags.Either);

                            if (CloseShip != null && sys.Shields < 9)
                            {
                                //ElsCond = false;
                                //WshPgt6 = false;
                                //WshPlt6 = false;
                                //NshPgt6 = false;
                                //NshPlt6 = true;
                                SetAutoStarShip(sys, false);
                                SetAutoShields(sys, false);
                                SetAutoStarShipType(sys, StarShipType.WarShip, 5, 3, false);
                                SetAutoStarShip(sys, true);
                            }
                            if (CloseShip != null && sys.Shields >= 9)
                            {
                                //ElsCond = false;
                                //WshPgt6 = false;
                                //WshPlt6 = false;
                                //NshPlt6 = false;
                                //NshPgt6 = true;
                                //------------------Close Capital Stalemate Breaker (no Visible Ships)
                                SetAutoStarShip(sys, false);
                                SetAutoShields(sys, false);
                                SetAutoStarShipType(sys, StarShipType.WarShip, 5, 3, false);
                                SetAutoStarShip(sys, true);
                            }
                            if (AttackingShip != null && sys.Shields < 9)
                            {
                                //ElsCond = false;
                                //WshPgt6 = false;
                                //NshPgt6 = false;
                                //NshPlt6 = false;
                                //WshPlt6 = true;
                                SetAutoStarShip(sys, false);
                                SetAutoShields(sys, true);
                                SetAutoStarShipType(sys, StarShipType.WarShip, 3, 3, false);
                                SetAutoStarShip(sys, true);
                            }
                            if (AttackingShip != null && sys.Shields >= 9)
                            {
                                //ElsCond = false;
                                //WshPlt6 = false;
                                //NshPgt6 = false;
                                //NshPlt6 = false;
                                //WshPgt6 = true;
                                //------------------Close Capital Stalemate Breaker
                                SetAutoStarShip(sys, false);
                                SetAutoShields(sys, true);
                                SetAutoStarShipType(sys, StarShipType.WarShip, 5, 3, false);
                                SetAutoStarShip(sys, true);
                            }
                        }
                        //Capital Build Normal
                        else if (sysValue <= 80 && CloseCaps == false)
                        {
                            //CCDE = false;
                            StarShipInfo AttackingShip = ClosestStarShip(sys.X, sys.Y, 5, FindShipFlags.Enemy, ShipKnownFlags.Either);
                            StarShipInfo CloseShip = ClosestStarShip(sys.X, sys.Y, 9, FindShipFlags.Enemy, ShipKnownFlags.Either);
                            if (AttackingShip != null)
                            {
                                SetAutoShields(sys, true);
                                SetAutoStarShipType(sys, StarShipType.Drone, 3, 0, false);
                                SetAutoStarShip(sys, true);
                            }

                            else if (CloseShip != null)
                            {
                                SetAutoShields(sys, false);
                                SetAutoStarShipType(sys, StarShipType.Drone, 6, 0, false);
                                SetAutoStarShip(sys, true);
                            }

                            else
                            {
                                SetAutoStarShipType(sys, StarShipType.WarShip, Random(8, 10), Random(5, 7), false);
                                SetAutoStarShip(sys, true);
                            }
                        }

                        //Capital Build Advanced
                        else if (sysValue > 80 && TechMultiple < 5 && CloseCaps == false)
                        {
                            //CCDE = false;
                            StarShipInfo AttackingShip = ClosestStarShip(sys.X, sys.Y, 6, FindShipFlags.Enemy, ShipKnownFlags.Either);
                            StarShipInfo CloseShip = ClosestStarShip(sys.X, sys.Y, 9, FindShipFlags.Enemy, ShipKnownFlags.Either);

                            //Set Autobuild to OFF

                            if (sys.AutoStarShip == true)
                                SetAutoStarShip(sys, false);

                            //------------------Defense Logic

                            if (AttackingShip != null)
                            {
                                SetAutoShields(sys, true);
                                for (int d = 0; d <= 3; d++)
                                {
                                    BuildStarShip(sys, StarShipType.Drone, Random(3, 6), 0, (Random(0, 100) > 85));
                                    BuildStarShip(sys, StarShipType.WarShip, Random(3, 6), Random(3, 4), false);
                                }
                            }
                            else if (CloseShip != null)
                            {
                                SetAutoShields(sys, false);
                                for (int d = 0; d <= 3; d++)
                                {
                                    BuildStarShip(sys, StarShipType.Drone, Random(6, 9), 0, (Random(0, 100) > 85));
                                    BuildStarShip(sys, StarShipType.WarShip, Random(6, 8), Random(3, 4), false);
                                }
                            }
                            else
                            {
                                SetAutoStarShipType(sys, StarShipType.TechShip, 3, 0, false);
                                SetAutoStarShip(sys, true); 
                            }
                        }

                        //Capital Build Power-up
                        else if (sysValue > 140 && CloseCaps == false)
                        {
                            //CCDE = false;
                            //Set Autobuild to OFF
                            if (sys.AutoStarShip == true)
                                SetAutoStarShip(sys, false);

                            //SpyShip Build Logic
                            if (NumWarShip > 15 && STtoSSratio > 30 && TechMultiple > 5)
                            {
                                BuildStarShip(sys, StarShipType.SpyShip, 20, 15, true);
                            }
                            //Power-up Warship Logic
                            if (NumWarShip >= 30 && STtoSSratio < 30)
                            {
                                BuildStarShip(sys, StarShipType.WarShip, 20, Random(5, MinWpn), true);

                            }
                            //Power-up Startillery Logic
                            if (NumWarShip < 30 && STtoSSratio < 30 && TechMultiple > 5)  //If brain is being brutalized
                            {
                                BuildStarShip(sys, StarShipType.StarTillery, 20, 8, true);
                            }
                            //---------------Defense Logic

                            StarShipInfo AttackingShip = ClosestStarShip(sys.X, sys.Y, 6, FindShipFlags.Enemy, ShipKnownFlags.Either);
                            StarShipInfo CloseShip = ClosestStarShip(sys.X, sys.Y, 9, FindShipFlags.Enemy, ShipKnownFlags.Either);
                            if (AttackingShip != null)
                            {
                                SetAutoShields(sys, true);
                                SetAutoStarShipType(sys, StarShipType.Drone, 3, 0, false);
                                SetAutoStarShip(sys, true);
                            }
                            else if (CloseShip != null)
                            {
                                SetAutoShields(sys, false);
                                SetAutoStarShipType(sys, StarShipType.Drone, 6, 0, false);
                                SetAutoStarShip(sys, true);
                            }
                            else
                            {
                                SetAutoStarShipType(sys, StarShipType.TechShip, 3, 0, false);
                                SetAutoStarShip(sys, true);
                            }
                        }
                        else
                        {
                            SetAutoStarShipType(sys, StarShipType.TechShip, 3, 0, false);
                            SetAutoStarShip(sys, true);
                        }
                    }
                    else if (sys.IsCapital == false)
                    {
                        //CCDE = false;
                        //----------------Techship Build Logic
                        if (sys.Value < 11 && TechMultiple < 5)
                        {
                            SetAutoStarShipType(sys, StarShipType.TechShip, 3, 0, false);
                            SetAutoStarShip(sys, true);
                        }

                        //------------------Alternate Low-end V10 Build Logic
                        if (sys.Value < 11 && NumTech <= 25 && NumWarShip < 15)
                        {
                            SetAutoStarShipType(sys, StarShipType.WarShip, Random(5, 8), Random(5, 7), false);
                            SetAutoStarShip(sys, true);
                        }

                        //-----------------Alternate High-end V10 Build Logic
                        if (sys.Value > 7 && sys.Value < 11 && NumTech > 25 && NumWarShip > 25)
                        {
                            SetAutoStarShipType(sys, StarShipType.WarShip, Random(8, 15), Random(6, 10), false);
                            SetAutoStarShip(sys, true);
                        }

                        //-----------------Default WarShip Build Logic
                        if (sys.Value >= 11 && sysValue < 140 && NumTech < 20)
                        {
                            SetAutoStarShipType(sys, StarShipType.WarShip, Random(8, 10), MinWpn + 1, false);
                            SetAutoStarShip(sys, true);
                        }

                        //-----------------Power-Up WarShip Build Logic
                        if (sys.Value > 7 && TechMultiple > 10)
                        {
                            SetAutoStarShipType(sys, StarShipType.WarShip, Random(8, 20), Random(5, MinWpn), false);
                            SetAutoStarShip(sys, true);
                        }

                        //-----------------Cloaked StarTillery Build Logic
                        if (sys.Value > 16 && TechMultiple > 10)
                        {
                            SetAutoStarShipType(sys, StarShipType.StarTillery, 20, Random(8, 15), true);
                            SetAutoStarShip(sys, true);
                        }

                        //-----------------Power-Up StarTillery Build Logic
                        if (sys.Value > 16 && TechMultiple > 5 && TechMultiple <= 10)
                        {
                            SetAutoStarShipType(sys, StarShipType.StarTillery, 20, Random(8, 15), false);
                            SetAutoStarShip(sys, true);
                        }

                        //------------------Power-up InkSpot Build Logic
                        if (sys.Value <= 7 && TechMultiple > 5)
                        {
                            SetAutoStarShipType(sys, StarShipType.InkSpot, 10, 0, true);
                            SetAutoStarShip(sys, true);
                        }
                        //------------------Power-up Drone Build Logic
                        if (sys.Value < 5 && TechMultiple > 5)
                        {
                            SetAutoStarShipType(sys, StarShipType.Drone, 10, 0, true);
                            SetAutoStarShip(sys, true);
                        }
                        if (sys.AutoStarShip == false)
                        {
                            SetAutoStarShipType(sys, StarShipType.WarShip, Random(8, 15), Random(5, MinWpn), true);
                            SetAutoStarShip(sys, true);
                        }
                        //----------------Defense Logic

                        StarSystemInfo EnemyStar = ClosestStarSystem(sys.X, sys.Y, 20, FindSystemFlags.Unfriendly);
                        StarShipInfo AttackingShip = ClosestStarShip(sys.X, sys.Y, 6, FindShipFlags.Enemy, ShipKnownFlags.Either);
                        StarShipInfo CloseShip = ClosestStarShip(sys.X, sys.Y, 9, FindShipFlags.Enemy, ShipKnownFlags.Either);
                        
                        if (AttackingShip != null && sys.Shields <= 6)
                        {
                            SetAutoShields(sys, true);
                            for (int d = 0; d <= 7; d++)
                                BuildStarShip(sys, StarShipType.Drone, 3, 0, false);
                        }

                        else if (AttackingShip != null && sys.Shields > 6)
                        {
                            SetAutoShields(sys, false);
                            for (int d = 0; d <= 7; d++)
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                        }

                        else if (CloseShip != null && sys.Shields <= 6)
                        {
                            SetAutoShields(sys, false);
                            for (int d = 0; d <= 7; d++)
                                BuildStarShip(sys, StarShipType.Drone, 3, 0, false);
                        }

                        else if (CloseShip != null && sys.Shields > 6)
                        {
                            SetAutoShields(sys, false);
                            for (int d = 0; d <= 7; d++)
                                BuildStarShip(sys, StarShipType.Drone, 6, 0, false);
                        }

                        else
                        {
                            if (NumTech < 50)
                                BuildStarShip(sys, StarShipType.TechShip, 3, 0, false);
                            if (EnemyStar != null)
                            {
                                SetAutoShields(sys, false);
                                SetAutoStarShipType(sys, StarShipType.WarShip, Random(8, 10), Random(5, 7), false);
                                SetAutoStarShip(sys, true);
                            }
                        }
                    }
                    else
                    {
                        SetAutoShields(sys, false);
                            BuildStarShip(sys, StarShipType.WarShip, Random(8, 10), Random(5, 7), false);
                        SetAutoStarShipType(sys, StarShipType.TechShip, 3, 0, false);
                        SetAutoStarShip(sys, true);
                    }
                }

            //--------------------------------Starship Movement-Targeting Logic----------------------------------------------------------

            double TargDist = 0;
            bool TargetPosX = false;
            bool TargetPosY = false;
            int TargetX = 0;
            int TargetY = 0;

            foreach (StarShipInfo sh in MyStarShips)
            {
                //-----------Warship Targeting Logic
                if (sh.ShipType == StarShipType.WarShip)
                {
                    if (_WarshipTarget[sh.Code] == null)
                    {
                        StarSystemInfo sys = null;
                        StarSystemInfo esys = null;
                        int closest = 32767;
                        int dist = 0;
                        for (int d = 0; d < StarSystems.Count; d++)
                        {

                            esys = StarSystems[d];
                            if (esys.OwnedByMe == false)
                            {
                                if (((_ShieldsLeft[esys] >= -2) & (expanded == true)) | ((_ShieldsLeft[esys] >= -15) & (expanded == false)))
                                {

                                    dist = DistanceQuick(esys.X, esys.Y, sh.X, sh.Y);
                                    if (expanded == true)
                                    {
                                        if ((esys.OwnerInt == -1 && closest > dist))
                                        {
                                            sys = esys;
                                            closest = dist;
                                        }
                                    }

                                    else
                                    {
                                        if (closest > dist)
                                        {
                                            sys = esys;
                                            closest = dist;
                                        }
                                    }
                                }
                            }
                        }

                        if (sys == null) sys = ClosestStarSystem(sh.X, sh.Y, 32767, FindSystemFlags.Unfriendly);

                        Target(sh, sys);
                        _WarshipTarget[sh.Code] = sys;
                        _ShieldsLeft[sys] = _ShieldsLeft[sys] - sh.Value;
                    }

                    else
                    {
                        StarSystemInfo sys = _WarshipTarget[sh.Code];
                        Target(sh, sys);
                        _ShieldsLeft[sys] = _ShieldsLeft[sys] - sh.Value;
                    }
                }
                //-------------InkSpot Targeting Logic
                else if (sh.ShipType == StarShipType.InkSpot)
                {
                    //target closest enemy ship

                    StarShipInfo isTarg = ClosestStarShip(sh.X, sh.Y, Int32.MaxValue, FindShipFlags.Enemy, ShipKnownFlags.Either);
                    if (isTarg != null)
                        Move(sh, isTarg.X + Random(-4, 4), isTarg.Y + Random(-4, 4));

                    //else, swarm closest enemy starsystem

                    else
                    {
                        StarSystemInfo targetSys = ClosestStarSystem(sh.X, sh.Y, Int32.MaxValue, FindSystemFlags.Unfriendly);
                        if (targetSys != null)
                            Move(sh, targetSys.X + Random(-4, 4), targetSys.Y + Random(-4, 4));
                        else
                            Move(sh, sh.X + Random(-4, 4), sh.Y + Random(-4, 4));
                    }
                }
                //-------------TechShip Movement Code
                else if (sh.ShipType == StarShipType.TechShip)
                {
                    int dx = sh.X < MapWidth / 2 ? 0 : MapWidth - 1;
                    int dy = sh.Y < MapHeight / 2 ? 0 : MapHeight - 1;
                    Move(sh, dx, dy);
                }
                //---------------Drone Movement Code
                else if (sh.ShipType == StarShipType.Drone)
                {
                    StarSystemInfo dfd = ClosestStarSystem(sh.X, sh.Y, 999, FindSystemFlags.Unfriendly);
                    Defend(sh, dfd);
                }
                //---------------EMine Movement Code
                else if (sh.ShipType == StarShipType.EMine)
                {
                    StarShipInfo isTarg = ClosestStarShip(sh.X, sh.Y, Int32.MaxValue, FindShipFlags.Enemy, ShipKnownFlags.Either);
                    if (isTarg != null)
                    {
                        StarSystemInfo dfe = ClosestStarSystem(sh.X, sh.Y, 999, FindSystemFlags.Unfriendly);
                        Defend(sh, dfe);
                    }
                    else   //Else, move toward enemy stars so more ships can be built
                    {
                        StarSystemInfo target = ClosestStarSystem(sh.X, sh.Y, 32767, FindSystemFlags.Unfriendly);
                        Move(sh, target.X + Random(-4, 4), target.Y + Random(-4, 4));
                    }
                }
                //---------------Startillery Targeting Code
                else if (sh.ShipType == StarShipType.StarTillery)
                {
                    //Target Closest Enemy Ship
                    StarShipInfo stTarg = ClosestStarShip(sh.X, sh.Y, 32767, FindShipFlags.Enemy, ShipKnownFlags.Either);
                    if (stTarg != null)
                    {

                        TargDist = Distance(sh.X, sh.Y, stTarg.X, stTarg.Y);

                        if (stTarg.X < MapWidth / 2)
                            //True indicates target starship located on the left half of the map.
                            TargetPosX = true;

                        if (stTarg.X >= MapWidth / 2)
                            //False indicates target starship located on the right half of the map.
                            TargetPosX = false;

                        if (stTarg.Y < MapHeight / 2)
                            //True indicates target starship located on the lower half of the map.
                            TargetPosY = true;

                        if (stTarg.Y >= MapHeight / 2)
                            //False indicates target starship located on the upper half of the map.
                            TargetPosY = false;

                        //Horizontal Positioning Code

                        if (TargDist <= 6 && TargetPosX == true)
                            TargetX = stTarg.X + 4;  //short range

                        if (TargDist <= 6 && TargetPosX == false)
                            TargetX = stTarg.X - 4;  //short range

                        if (TargDist > 6 && TargDist <= 10 && TargetPosX == true)
                            TargetX = stTarg.X + 5;  //medium range

                        if (TargDist > 6 && TargDist <= 10 && TargetPosX == false)
                            TargetX = stTarg.X - 5;  //medium range

                        if (TargDist > 10 && TargetPosX == true)
                            TargetX = stTarg.X + 8;  //long range

                        if (TargDist > 10 && TargetPosX == false)
                            TargetX = stTarg.X - 8;  //long range

                        //Vertical Positioning Code

                        if (TargDist <= 6 && TargetPosY == true)
                            TargetY = stTarg.Y + 4;  //short range

                        if (TargDist <= 6 && TargetPosY == false)
                            TargetY = stTarg.Y - 4;  //short range

                        if (TargDist > 6 && TargDist <= 10 && TargetPosY == true)
                            TargetY = stTarg.Y + 5;  //medium range

                        if (TargDist > 6 && TargDist <= 10 && TargetPosY == false)
                            TargetY = stTarg.Y - 5;  //medium range

                        if (TargDist > 10 && TargetPosY == true)
                            TargetY = stTarg.Y + 8;  //long range

                        if (TargDist > 10 && TargetPosY == false)
                            TargetY = stTarg.Y - 8;  //long range

                        Move(sh, TargetX, TargetY);
                    }

                    else   //Else, move toward enemy stars so more ships can be built
                    {
                        StarSystemInfo target = ClosestStarSystem(sh.X, sh.Y, 32767, FindSystemFlags.Unfriendly);
                        Move(sh, target.X + Random(-4, 4), target.Y + Random(-4, 4));
                    }

                }
                //---------------SpyShip Movement Code
                else if (sh.ShipType == StarShipType.SpyShip)
                {
                    //Target Closest Enemy Ship
                    StarShipInfo spTarg = ClosestStarShip(sh.X, sh.Y, 32767, FindShipFlags.Enemy, ShipKnownFlags.Either);
                    if (spTarg != null)
                    {

                        TargDist = Distance(sh.X, sh.Y, spTarg.X, spTarg.Y);

                        if (spTarg.X < MapWidth / 2)
                            //True indicates target starship located on the left half of the map.
                            TargetPosX = true;

                        if (spTarg.X >= MapWidth / 2)
                            //False indicates target starship located on the right half of the map.
                            TargetPosX = false;

                        if (spTarg.Y < MapHeight / 2)
                            //True indicates target starship located on the lower half of the map.
                            TargetPosY = true;

                        if (spTarg.Y >= MapHeight / 2)
                            //False indicates target starship located on the upper half of the map.
                            TargetPosY = false;

                        //Horizontal Positioning Code

                        if (TargDist <= 5 && TargetPosX == true)
                            TargetX = spTarg.X + 6;  //short range

                        if (TargDist <= 5 && TargetPosX == false)
                            TargetX = spTarg.X - 6;  //short range

                        if (TargDist > 5 && TargDist <= 10 && TargetPosX == true)
                            TargetX = spTarg.X + 6;  //medium range

                        if (TargDist > 5 && TargDist <= 10 && TargetPosX == false)
                            TargetX = spTarg.X - 6;  //medium range

                        if (TargDist > 10 && TargetPosX == true)
                            TargetX = spTarg.X + 9;  //long range

                        if (TargDist > 10 && TargetPosX == false)
                            TargetX = spTarg.X - 9;  //long range

                        //Vertical Positioning Code

                        if (TargDist <= 5 && TargetPosY == true)
                            TargetY = spTarg.Y + 6;  //short range

                        if (TargDist <= 5 && TargetPosY == false)
                            TargetY = spTarg.Y - 6;  //short range

                        if (TargDist > 5 && TargDist <= 10 && TargetPosY == true)
                            TargetY = spTarg.Y + 6;  //medium range

                        if (TargDist > 5 && TargDist <= 10 && TargetPosY == false)
                            TargetY = spTarg.Y - 6;  //medium range

                        if (TargDist > 10 && TargetPosY == true)
                            TargetY = spTarg.Y + 9;  //long range

                        if (TargDist > 10 && TargetPosY == false)
                            TargetY = spTarg.Y - 9;  //long range

                        Move(sh, TargetX, TargetY);
                    }

                    else   //Else, move toward enemy stars so more ships can be built
                    {
                        StarSystemInfo target = ClosestStarSystem(sh.X, sh.Y, 32767, FindSystemFlags.Unfriendly);
                        Move(sh, target.X + Random(-7, 7), target.Y + Random(-7, 7));
                    }

                }
                else
                {
                    StarSystemInfo target = ClosestStarSystem(sh.X, sh.Y, 32767, FindSystemFlags.Unfriendly);
                    Move(sh, target.X + Random(-4, 4), target.Y + Random(-4, 4));
                }
            }   
        }
    }
}
